<script type="text/javascript">
$(document).ready(function(){
	$("#mode").change(function(){
		var mode = $(this).val();
		if(mode=='month'){
			$("#tr_month").show();
			$("#tr_year").hide();
		}
		else if(mode=='year'){
			$("#tr_month").hide();
			$("#tr_year").show();
		}
	})
	
	var modeSelect = $("#mode").val();
	if(modeSelect=='month'){
		$("#tr_month").show();
		$("#tr_year").hide();
	}
	else if(modeSelect=='year'){
		$("#tr_month").hide();
		$("#tr_year").show();
	}
	
	var count = 0;
	$(".count").each(function(){
		count += parseInt($(this).text());
	});
	$(".rscount").text(count);
})
</script>
<h2>รายงานจำนวนผู้เข้าชมเว็บไซต์</h2>
<form>
	<div id="advancedSearch">
		<table width="95%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td colspan="2"  class="topic_advancedSearch"><img src="images/topic_advancedSearch2.gif" /></td>
		  </tr>
		  <tr>
			<td width="180"><br>รูปแบบ</td>
			<td>
				<select name="mode" id="mode">
					<option value="month" selected="selected" >รายเดือน</option>
					<option value="year" <? if(@$_GET['mode']=='year')echo 'selected="selected"';?> >ปี</option>
				</select>
			</td>
		  </tr>	
		  <tr id="tr_month">
			<td width="180"><br>ปี</td>
			<td>
				<select name="start_year_m">
					<? 
					$cyear= date("Y");
					for($i=0;$i<10;$i++):
						$year=$cyear-$i;
					?>
					<option value="<?=$year;?>" <?=(@$_GET['start_year_m'] == $year)?"selected=selected":"";?>><?=$year+543;?></option>
					<? endfor;?>					
				</select>							
			</td>
		  </tr>	
		  <tr id="tr_year" <? if(@$_GET['mode']!='year')echo 'style="display:none"';?> >
			<td width="180"><br>ปี</td>
			<td>
				<select name="start_year">
					<? 
					$cyear= date("Y");
					for($i=0;$i<10;$i++):
						$year=$cyear-$i;
					?>
					<option value="<?=$year;?>" <?=(@$_GET['start_year'] == $year)?"selected=selected":"";?>><?=$year+543;?></option>
					<? endfor;?>					
				</select>
				
				ถึง 
				
				<select name="end_year">
					<? 
					$cyear= date("Y");
					for($i=0;$i<10;$i++):
						$year=$cyear-$i;
					?>
					<option value="<?=$year;?>" <?=(@$_GET['end_year'] == $year)?"selected=selected":"";?>><?=$year+543;?></option>
					<? endfor;?>					
				</select>
			</td>
		  </tr>					  
		  <tr>
			<td>&nbsp;</td>
			<td><input type="submit" name="submit" value="ค้นหา" ></td>
		  </tr>
		</table>
	</div>
</form>

<div id="main-content">
	<table class="tbContainer">
		<tr>
			<td>
				<? if(@$_GET['mode']=='month'): ?>
				<table border="0" cellpadding="10" cellspacing="0" width=""  >
						<tr class="ui-state-error">
							<td colspan="2" class="ui-state-error tdlist" style="text-align:center;">ปี <?=$_GET['start_year_m']+543?></td>
						</tr>
						<tr class="ui-state-error">
							<td class="ui-state-error tdlist" style="width:150px;">เดือน</td>
							<td class="ui-state-error tdlist" style="text-align: center;width:250px;">จำนวนผู้เข้าชม (คน)</td>							
						</tr>
						<?
						$month = get_month();
						foreach($month as $key=>$item):
							if ($key > 0):;
						?>
						<tr>
							<td class="tdlist"><?=$item?></td>
							<td class="tdlist count" style="text-align: right;padding-right: 20px;">
								<?php
									$sql = "SELECT
count(guest_log.id) as login_count
FROM
guest_log
where year(created) = ".$_GET['start_year_m']." and month(created) = ".$key;
									$dl = $this->suggess_result->get($sql);
									echo $dl[0]['login_count'];
								?>
							</td>
							
						</tr>
						<?
							endif; 
						endforeach;
						?>
						<tr class="ui-state-error">
							<td class="ui-state-error tdlist" style="width:150px;">รวม</td>
							<td class="ui-state-error tdlist rscount" style="text-align: right;padding-right: 20px;"></td>							
						</tr>
				</table>
				<? endif;?>
				
				<? if(@$_GET['mode']=='year'): ?>
				<table border="0" cellpadding="10" cellspacing="0" width=""  >
						<tr class="ui-state-error">
							<td class="ui-state-error tdlist" style="width:150px;">ปี</td>
							<td class="ui-state-error tdlist" style="text-align: center;width:250px;">จำนวนผู้เข้าชม (คน)</td>
							
						</tr>
						<?			
						$diffYear = ($_GET['end_year'] - $_GET['start_year'])+1;			
						for($i=0;$i<$diffYear;$i++):
							 $year = ($_GET['end_year'] - $i)+543;
						?>
						<tr>
							<td class="tdlist"><?=$year;?></td>
							<td class="tdlist count" style="text-align: right;padding-right: 20px;">
								<?php
									$yearselect = $year-543;
									$sql = "SELECT
count(guest_log.id) as login_count
FROM
guest_log
where year(created) = ".$yearselect;
									$dl = $this->suggess_result->get($sql);
									echo $dl[0]['login_count'];
								?>
							</td>
							
						</tr>
						<? endfor;?>
						<tr class="ui-state-error">
							<td class="ui-state-error tdlist" style="width:150px;">รวม</td>
							<td class="ui-state-error tdlist rscount" style="text-align: right;padding-right: 20px;"></td>							
						</tr>
				</table>
				<? endif;?>
			</td>
		</tr>
	</table>
</div>    